package com.sharekitchen.controller;

import com.sharekitchen.common.R;
import com.sharekitchen.entity.Admin;
import com.sharekitchen.entity.User;
import com.sharekitchen.service.UserService;
import com.sharekitchen.utils.util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public R<String> register(@RequestBody User user) {
//        检验用户名和密码为空
        if (util.isEmptyOrNull(user.getUsername()) || util.isEmptyOrNull(user.getPassword())) {
            return R.fail("用户名或密码不可为空,注册失败");
        }
//            不存在该用户
        if (userService.existUsername(user.getUsername())) {
            return R.fail("用户名重复,注册失败");
        }
//             通过校验
        if (userService.addUser(user)) {
            log.info("注册成功"+user);
            return R.ok("注册成功~", null);
        }
        return R.error("注册失败~");
    }

    @PostMapping("/login")
    public R<String> login(@RequestBody User user) {
        String username = user.getUsername();
        String password = user.getPassword();
//        用户名密码不为空
        if (util.isEmptyOrNull(username) || util.isEmptyOrNull(password)) {
            return R.fail("用户名或密码不可为空,登录失败");
        }
        //            不存在该用户
        if (!(userService.existUsername(username))) {
            return R.fail("用户不存在,登录失败");
        }
        //        通过校验
        if (userService.findUserByName(username).getPassword().equals(password)) {
            log.info("登陆成功"+user);
            return R.ok("登陆成功~", null);
        }
        return R.error("密码错误,登录失败");
    }

    @GetMapping("/all")
    public R<List<User>> getAllUser() {
        List<User> allUser = userService.findAllUser();
        if (allUser != null){
            log.info(allUser.toString());
            return R.ok("查询成功",allUser);
        }
        return R.error("查询失败");
    }


    @GetMapping("/name")
    public R<User> getUserByName(@RequestParam String username) {
        if (util.isEmptyOrNull(username)) {
            return R.fail("用户名不能为空~,查询失败");
        }
        if (!userService.existUsername(username)) {
            return R.fail("用户不存在");
        }
        User user = userService.findUserByName(username);
        if (user != null) {
            log.info("查询成功"+user);
            return R.ok("查询成功~", user);
        }
        return R.error("查询失败");
    }

    @DeleteMapping("/delete")
    public R<String> deleteAdminByName(@RequestParam String username) {
        if (util.isEmptyOrNull(username)) {
            return R.fail("用户名不能为空,删除失败");
        }
        if (!userService.existUsername(username)) {
            return R.fail("该用户不存在,删除失败~");
        }
        if (userService.deleteUserByName(username)) {
            log.info("删除成功");
            return R.ok("删除成功", null);
        }
        return R.error("删除失败");
    }
}
